package gov.nhso.prov.acc.apps.acc6;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.isud.core.ISUD;
import com.isud.core.ISUDFactory;

import gov.nhso.prov.acc.core.DBConnection;
import gov.nhso.prov.acc.utils.Utility;

public class ACC60102 extends Utility{
	public String url = "/apps/acc6/ACC60102A.jsp";

	public ACC60102(HttpServletRequest request, HttpServletResponse response) {
		this.message(request).clearMsg();
		String job = this.getAttr(request, "job");
		if(job.equals("init")){
			this.init(request);
		}else if(job.equals("search")){
			this.search(request);
		}
	}
	private String init(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			
			request.setAttribute("listBudgetYear", this.dataUtil(dao).lsBudgetYear());
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	@SuppressWarnings("unchecked")
	private String search(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			
			String sql = "";
			List<Map<String, String>> list = null;
			String table = this.schema + ".ACC6_ALLOCATE";
			String budgetYear = this.getAttr(request, "budgetYear");
			
			sql = "SELECT ALLOCATE_CODE,ALLOCATE_NAME,ALLOCATE_LEVEL,SAP_CODE "
				+ "FROM "+table+" "
				+ "WHERE PARENT_CODE IS NOT NULL AND ALLOCATE_LEVEL IS NOT NULL AND ACTIVE = 'Y' AND BUDGET_YEAR = '"+budgetYear+"'  ORDER BY ALLOCATE_SEQ ASC";
			list = dao.listDataSql(sql);
			
			request.setAttribute("listData", list);
			request.setAttribute("budgetYear", budgetYear);
			request.setAttribute("listBudgetYear", this.dataUtil(dao).lsBudgetYear());
			this.url = "/apps/acc6/ACC60102A.jsp";
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
}
